package exam.web.action;

import java.util.Date;

import exam.common.util.LongUtils;
import exam.common.util.NullCheckUtil;
import exam.model.entity.TStudentEntity;
import exam.model.entity.TUserEntity;
import exam.web.action.base.StudentActionBase;

public class EditStudentAction extends StudentActionBase {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		String btUpdate = request.getParameter("btUpdate");
		if(NullCheckUtil.isNUll(btUpdate)) {
			String studentId = request.getParameter("studentId");
			if(NullCheckUtil.isNUll(studentId)) {
				addActionError("#studentId không hợp lệ");
				return ERROR;
			}
			settStudentEntity(studentDAO.selectByPk(LongUtils.convertStringToLong(studentId)));
			return INPUT;
		} else {
			TStudentEntity studentEntity = studentDAO.selectByPk(tStudentEntity.getStudentId());
			if(NullCheckUtil.isNotNull(studentEntity)) {
				studentEntity.setStudentCd(tStudentEntity.getStudentCd());
				studentEntity.setFirstName(tStudentEntity.getFirstName());
				studentEntity.setLastName(tStudentEntity.getLastName());
				studentEntity.setBirthday(tStudentEntity.getBirthday());
				studentEntity.setAddress(tStudentEntity.getAddress());
				studentEntity.setGender(tStudentEntity.getGender());
				studentEntity.setCourse(tStudentEntity.getCourse());
				studentEntity.setStudentClass(tStudentEntity.getStudentClass());
				studentEntity.setUpdateDate(new Date());
				studentEntity.setUserEntity(
						new TUserEntity(tStudentEntity.getUserId()));
				studentDAO.updateStudent(studentEntity);
				addActionMessage("Cập thông tin thí sinh thành công");
			} else {
				addActionError("Cập nhật thông tin thí sinh thất bại");
			}
			return SUCCESS;
		}
	}
}
